package com.css.gdzj.gdzjbxgd.modules.statisticAnalysis.controller;

import com.css.gdzj.gdzjbxgd.common.util.R;
import com.css.gdzj.gdzjbxgd.common.util.StringHelper;
import com.css.gdzj.gdzjbxgd.modules.reportcontral.repository.BxgdReportContralRepository;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.BxgdReportInfoRepository;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.service.BxgdReportInfoService;
import com.css.gdzj.gdzjbxgd.modules.statisticAnalysis.service.StaticAnalysisService;
import com.css.gdzj.gdzjbxgd.modules.userinfo.repository.UserInfoNativeRepository;
import com.css.gdzj.gdzjbxgd.modules.userinfo.repository.UserInfoRepository;
import com.css.gdzj.gdzjbxgd.modules.userinfo.repository.entity.UserInfoEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/statisticAnalysis")
public class StatisticAnalysisController {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private StaticAnalysisService staticAnalysisService;
    @Autowired
    private BxgdReportInfoService bxgdReportInfoService;
    @Autowired
    UserInfoNativeRepository userInfoNativeRepository;
    @Autowired
    BxgdReportInfoRepository bxgdReportInfoRepository;
    @Autowired
    BxgdReportContralRepository bxgdReportContralRepository;
    @Autowired
    UserInfoRepository userInfoRepository;

    @GetMapping("/getReportUserInfos")
    public R getReportUserInfos(
            @RequestParam(value = "userIsReport", required = false) String userIsReport,
            @RequestParam(value = "reportYear", required = false) String reportYear,
            @RequestParam(value = "userUnit", required = false) String userUnit,
            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        try {
            if (StringHelper.isEmpty(reportYear)) {
                reportYear = bxgdReportContralRepository.queryReportYear();
            }
            Pageable pageable  = PageRequest.of(pageNo - 1, pageSize);
            int isReport = 0, notReport = 0, total = 0;
            List<Object> userInfoEntities = null;
            Page<UserInfoEntity> userInfoEntities1 = null;

            if (!StringHelper.isEmpty(userIsReport)) {
                userInfoEntities1 = staticAnalysisService.getUserReportInfos1(userIsReport, reportYear, userUnit, pageable);
                int elements = Math.toIntExact(userInfoEntities1.getTotalElements());
                if (userIsReport.equals("0")) {
                    isReport = elements;
                } else {
                    notReport = elements;
                }
                total = isReport + notReport;
            } else {//无用户是否填报的条件
                userInfoEntities = staticAnalysisService.getUserReportInfos(reportYear, userUnit, pageNo, pageSize);
                Map<String, Object> mapUser = userInfoNativeRepository.queryUserStatistic(userUnit, pageNo, pageSize);
                Map<String, Object> mapUsers = userInfoNativeRepository.queryUserStatisticCount(userUnit);
                BigInteger total1 = (BigInteger) mapUser.get("total");
                if (total1 != null) {
                    total = total1.intValue();
                }
                List<UserInfoEntity> userInfoss = (List) mapUsers.get("listUserInfo");
                String userId = null;
                for (UserInfoEntity userInfo : userInfoss) {
                    userId = userInfo.getUserId();
                    int count = 0;
                    count = bxgdReportInfoRepository.getUserReportCount(userId, reportYear);
                    if (count > 0) {
                        isReport++;
                    }
                }
                notReport = total - isReport;
            }

            return R.ok().put("userIsReport", userIsReport).put("userInfoEntities", userInfoEntities).put("userInfoEntities1", userInfoEntities1).put("isReport", isReport).put("notReport", notReport).put("total", total);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return R.error();
        }
    }

    @RequestMapping("/getUserAndReportInfos")
    public R getInfos1(@RequestBody Map<String, String> params,
                       @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize", required = false, defaultValue = "2000") Integer pageSize) {

        int total = 0;
        try {
//            pageSize = Integer.MAX_VALUE;
            Pageable pageable = new PageRequest(pageNo - 1, pageSize);
            String reportYear = params.get("reportYear");
            String userUnit = params.get("userUnit");
            if (StringHelper.isEmpty(reportYear)) {
                reportYear = bxgdReportContralRepository.queryReportYear();
            }
            Page<UserInfoEntity> isReportP = userInfoRepository.getIsReportP(reportYear, userUnit, pageable);
            total = Math.toIntExact(isReportP.getTotalElements());

            List<Object> objects = bxgdReportInfoService.queryAllReportInfos1(params, pageNo, pageSize, pageable);

            return R.ok().put("objects", objects).put("total", total);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return R.error();
        }
    }
}
